using System;

namespace PhotoGallery.Configuration
{
    [System.Configuration.ConfigurationCollection(typeof(NamedConfigurationElementBase),
        CollectionType = System.Configuration.ConfigurationElementCollectionType.AddRemoveClearMap)]
    public abstract class NamedConfigurationElementCollectionBase<T> : System.Configuration.ConfigurationElementCollection
        where T : NamedConfigurationElementBase, new()
    {
        public T this[int index] { get { return (T)base.BaseGet(index); } }

        public new T this[string name] { get { return (T)base.BaseGet(name); } }

        protected override System.Configuration.ConfigurationElement CreateNewElement()
        {
            return new T();
        }

        protected override object GetElementKey(System.Configuration.ConfigurationElement element)
        {
            return (element as T).Name;
        }
    }

    public class DALProviderConfigurationElementCollection : NamedConfigurationElementCollectionBase<DALProviderConfigurationElement>
    {
        private const string DefaultItem = "default";

        [System.Configuration.ConfigurationProperty(DefaultItem, IsRequired = true, DefaultValue = "SqlServerProvider")]
        protected string DefaultItemName
        {
            get
            {
                return base.ElementInformation.Properties[DefaultItem].Value.ToString();
            }
        }

        public DALProviderConfigurationElement Default
        {
            get
            {
                return base[DefaultItemName] as DALProviderConfigurationElement;
            }
        }
    }
}